package com.smiier.skin.util;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import cn.o.app.ui.OActionSheet;
import cn.o.app.ui.OActionSheet.OActionItem;
import cn.o.app.ui.OActionSheet.OnActionItemClickListener;
import cn.skinapp.R;

import com.evan.common.share.ShareQQUtils;
import com.evan.common.share.ShareWXUtils;
import com.evan.common.share.ShareWeiboUtils;
import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.exception.WeiboShareException;

public class ShareUtil {

	public static void share(final Context context, final String url,
			final Object title, final Object description) {
		ArrayList<OActionItem> dataProvider = new ArrayList<OActionSheet.OActionItem>();
		OActionItem item = null;
		item = new OActionItem();
		item.setText("分享至微信好友");
		dataProvider.add(item);
		item = new OActionItem();
		item.setText("分享至微信朋友圈");
		dataProvider.add(item);
		item = new OActionItem();
		item.setText("分享至新浪微博");
		dataProvider.add(item);
		item = new OActionItem();
		item.setText("分享给QQ好友");
		dataProvider.add(item);
		OActionSheet<OActionItem> actionSheet = new OActionSheet<OActionItem>(
				context);
		actionSheet.setCancel("取消");
		actionSheet.setDataProvider(dataProvider);
		actionSheet.show(false, true);
		actionSheet
				.setOnActionItemClickListener(new OnActionItemClickListener<OActionSheet.OActionItem>() {

					@Override
					public void onItemClick(OActionSheet<OActionItem> sheet,
							View v, int position, OActionItem dataItem) {
						try {
							if (position == 0 || position == 1) {
								ShareWXUtils.getInstance(context).share2WXWEB(
										url, title, description,
										position == 0 ? false : true);
							} else if (position == 2) {
								try {
									ShareWeiboUtils shareWeiboUtils = ShareWeiboUtils
											.getInstance(context);
									IWeiboShareAPI mWeiboShareAPI = WeiboShareSDK
											.createWeiboAPI(
													context,
													GlobalSettings.WEIBO_APP_KEY_PATIENT);
									// 检查微博客户端环境是否正常，如果未安装微博，弹出对话框询问用户下载微博客户端
									if (mWeiboShareAPI.isWeiboAppInstalled()) {
										Bitmap bitmap = BitmapFactory
												.decodeResource(
														context.getResources(),
														R.drawable.ic_launcher);
										WebpageObject webpageObject = shareWeiboUtils
												.getWebpageObj(title,
														description, bitmap,
														url);
										WeiboMultiMessage weiboMessage = new WeiboMultiMessage();
										weiboMessage.mediaObject = webpageObject;
										SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();

										request.transaction = String
												.valueOf(System
														.currentTimeMillis());

										request.multiMessage = weiboMessage;
										shareWeiboUtils.share(webpageObject);
										// mWeiboShareAPI.sendRequest(
										// (Activity) context, request);

									}
								} catch (WeiboShareException e) {
									e.printStackTrace();
								}
							} else if (position == 3) {
								ShareQQUtils.getInstance(context).share(url,
										title, description);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

				});
	}
}
